import requests
import sys
Source = ['DXDL','LOCAL']

from qqwry import QQwry 
qqwry_db = QQwry() 
qqwry_db.load_file('data/qqwry.dat', loadindex=False)

class Base():
    @classmethod
    def get_proxy(cls):
        pass

    @classmethod
    def parse_proxy(cls,ip,port):
        ret = qqwry_db.lookup(ip)
        if ret:
            return {'ip':ip,'port':port,'country':ret[0],'area':ret[1]}
        else:
            return {'ip':ip,'port':port,'country':'unknow','area':'unknow'}


class DXDL(Base):

    @classmethod
    def get_proxy(cls):

        proxies = []
        try:
            req = requests.get('http://vtp.daxiangdaili.com/ip/?tid=555379722203134&num=1000')
            for line in req.text.split('\n'):
                if line:
                    px = line.split(':')
                    proxies.append(cls.parse_proxy(px[0],px[1]))

        except Exception as e:
            sys.stdout.write(str(e) + "\r\n")
            sys.stdout.flush()

        return proxies

class LOCAL(Base):

    @classmethod
    def get_proxy(cls):

        proxies = []
        try:
            req = requests.get('http://192.168.1.220:9001/?types=0')
            for row in req.json():
                if row:
                    proxies.append(cls.parse_proxy(row[0],row[1]))

        except Exception as e:
            sys.stdout.write(str(e) + "\r\n")
            sys.stdout.flush()

        return proxies



if __name__ == '__main__':
    print(DXDL.get_proxy())